package com.wuliu.business.login;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.wuliu.util.BaseUtil;
import com.wuliu.util.DbPool;
import com.wuliu.model.sys.User;

public class LoginObj {
	//根据登录用户名和密码获得用户实体
	   public static User getLogin(String userAccount,String userPwd ) {
	       
			StringBuffer sb = new StringBuffer();
			sb.append(" SELECT * FROM WL_USER WHERE USER_ACCOUNT=? AND USER_PWD=? ");
			Connection con = null;
			PreparedStatement pstmt=null;
			ResultSet rs=null;
			User user = null;
			try{ //连接数据库
	   		con = DbPool.getConnection();
	   		pstmt = con.prepareStatement(sb.toString());
	   		pstmt.setString(1,userAccount);
	   		pstmt.setString(2, userPwd);
	   		rs =  pstmt.executeQuery();
	   		String rightCode = "" ;
	   		String roleIds = "" ;
	   		String roleNames = "";
				while(rs.next()){
					user = new User();
					user.setUserId(rs.getString("USER_ID"));
					user.setUserAccount(rs.getString("USER_ACCOUNT"));
					user.setUserPwd(rs.getString("USER_PWD"));
					user.setUserName(rs.getString("USER_NAME"));
					user.setUserDesc(rs.getString("USER_DESC"));
					user.setUserTel(rs.getString("USER_TEL"));
					user.setUserSpot(rs.getString("USER_SPOT"));
					user.setAddBy(rs.getString("ADD_BY"));
					user.setAddDate(rs.getString("ADD_DATE"));
					user.setAddTime(rs.getString("ADD_TIME"));
					user.setUpdBy(rs.getString("UPD_BY"));
					user.setUpdDate(rs.getString("UPD_DATE"));
					user.setUpdTime(rs.getString("UPD_TIME"));
					user.setActiveFlag(rs.getString("ACTIVE_FLAG"));
				}
			} catch(Exception ee){
				try{
					System.out.println(ee +"，，getLogin");
				}
				catch(Exception e){
					System.out.println(e+"。。getLogin");
				}
			} finally{
				try {
		             if (rs != null)
		                rs.close();
		             if (pstmt != null)
		                pstmt.close();
		             if(con!=null)
						con.close();
		         } catch (Exception e) { 
		             System.out.println(e);
		         }
			}
			return user;
	   }
	   
	   public static User getUserAccount(String userAccount) {
		   StringBuffer sb = new StringBuffer();
			sb.append("SELECT A.USER_ACCOUNT,A.USER_PWD FROM WL_USER A WHERE A.USER_ACCOUNT='"+userAccount+"' ");
			Connection con = null;
			PreparedStatement stmt=null;
			ResultSet dest=null;
			User vo = null;
			try{ //连接数据库
	   		con = DbPool.getConnection();
	   		stmt = con.prepareStatement(sb.toString());
	   		dest =  stmt.executeQuery();
		   		while(dest.next()){
					vo = new User();
					vo.setUserAccount(BaseUtil.getStr(dest.getString("USER_ACCOUNT")));
					vo.setUserPwd(BaseUtil.getStr(dest.getString("USER_PWD")));
		   		}
	   		}catch(SQLException sqlE){
				sqlE.printStackTrace();
			} 
			finally{
				DbPool.close(con, stmt, dest);
			}
		   return vo;
	   }
}
